package com.zl.quickdoc.resolver.converter.impl;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import com.zl.quickdoc.constant.QuickDocC;
import com.zl.quickdoc.model.ApiField;
import com.zl.quickdoc.resolver.converter.AbstractJavaDocTag;
import com.zl.quickdoc.resolver.converter.JavaParserTagConverter;

/**
 * 针对@param的转换器
 * @author zhouliang
 *
 */
public class ParamTagConverter implements JavaParserTagConverter {

    @SuppressWarnings("unchecked")
    @Override
    public AbstractJavaDocTag<ApiField> converter(String comments) {
        DefaultStringParserTagConverterImpl stringtag = new DefaultStringParserTagConverterImpl();
        AbstractJavaDocTag<String> docTag = stringtag.converter(comments);
        
        String val = (String) docTag.getValues();
        String[] array = val.split("[ \t]+");
       
        ApiField field = new ApiField();
        field.setTitle("");
        field.setName("");
        field.setShowType("String");
        field.setFactType(field.getShowType());
        field.setDescription("");
        field.setRequire(false);
        
        //username:String 用户名|Y|默认值|描述
        String[] names= array[0].split("\\:");
        field.setName(names[0]);
        if(names.length>1) {
            field.setShowType(names[1]);
            field.setFactType(field.getShowType());
        }
        
        String title = "";
        String defaultValue = "";
        String comment = "";
        boolean require = false;
        if (array.length > 1) {
            String[] descs = array[1].split("\\|",4);
            if (descs.length > 0) {
                title = descs[0];
                
                if (descs.length > 3) {
                    String requireString = descs[1].trim();
                    require = QuickDocC.YES_ZH.equals(requireString) || QuickDocC.YES_EN.equalsIgnoreCase(requireString);
                    defaultValue = descs[2];
                    comment = descs[3];
                }
                else if(descs.length > 1){
                    Pattern p = Pattern.compile("^ *(" + QuickDocC.YES_ZH+"|"+QuickDocC.YES_EN+")\\b *$", Pattern.CASE_INSENSITIVE);
                    Matcher m = p.matcher(descs[1]);
                    if(m.find()) {
                        String requireString = m.group().trim();
                        require = QuickDocC.YES_ZH.equals(requireString) || QuickDocC.YES_EN.equalsIgnoreCase(requireString);
                        if (descs.length > 2) {
                            defaultValue = descs[2];
                        }
                    }else {
                        defaultValue = descs[1];
                        if (descs.length > 2) {
                            comment = descs[2];
                        }
                    }
                }
            }
        }
        if(comment.contains("|")) {
            comment = comment.replaceAll("\\|("+QuickDocC.YES_EN+"|"+QuickDocC.YES_EN+"|"+QuickDocC.NOT_EN+"|"+QuickDocC.NOT_EN+")$","");
        }
        String title2=title;
        if(title.contains("|")) {
            title = title.split("\\|")[0];
        }
        field.setTitle(title.length()>25?title.substring(0,25):title);
        field.setDescription(comment);
        if(StringUtils.isNotBlank(title2) && StringUtils.isBlank(comment)) {
            field.setDescription(title2);
        }
        else if(StringUtils.isNotBlank(title2) && !comment.startsWith(title)) {
            field.setDescription(title2+"："+ comment);
        }
        field.setRequire(require);
        field.setDefaultValue(defaultValue);
        
        return new ParamTagImpl(docTag.getTagName(),  field);
    }
    
    public class ParamTagImpl extends AbstractJavaDocTag<ApiField> {
        
        private ApiField field;
        
        public ParamTagImpl(String tagName, ApiField field) {
            super(tagName);
            this.field = field;
        }

        @Override
        public ApiField getValues() {
            return field;
        }
    }
}
